<script setup lang="ts">
  import { nextTick, ref, watch } from 'vue'
  import { useSendInviteCodeRecordsService } from '../services'
  import Pagination from '@/components/Pagination/index.vue'
  import HStatus from '@/components/common/status/HStatus.vue'
  import { HModal } from '@/components/common'
  const props = defineProps({
    modelValue: {
      type: Boolean,
      default: false,
    },
  })

  const emits = defineEmits<{
    (e: 'update:modelValue', value: boolean): void
  }>()

  // 分页组件实例
  const paginationRef = ref<InstanceType<typeof Pagination> | null>(null)

  // 使用服务
  const { loading, getList, total, recordList } = useSendInviteCodeRecordsService()

  // 组装filter
  const getFilter = async () => {
    const result = {
      limit: 50,
      page: 1,
    }
    await nextTick()
    if (paginationRef.value) {
      result.limit = paginationRef.value.size
      result.page = paginationRef.value.current
    }
    return result
  }

  // 弹窗显示状态
  const visible = ref(false)

  // 关闭弹窗事件
  const onClose = () => {
    visible.value = false
    emits('update:modelValue', visible.value)
  }

  // 获取列表
  const __getList = async () => {
    const filter = await getFilter()
    getList(filter.limit, filter.page)
  }

  watch(
    () => props.modelValue,
    (value) => {
      visible.value = value
      if (value) {
        __getList()
      }
    },
  )
</script>

<template>
  <div>
    <HModal :width="800" title="邀请记录" v-model="visible" @close="onClose">
      <el-table
        row-key="id"
        border
        style="width: 100%"
        max-height="250"
        :data="recordList"
        v-loading="loading"
      >
        <el-table-column fixed prop="receivePhone" label="手机号码" width="120" />
        <el-table-column show-overflow-tooltip label="短信内容" prop="content" min-width="260">
          <template #default="scope">
            <div style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis">
              {{ scope.row.content }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="success" label="发送状态" width="80">
          <template #default="scope">
            <HStatus v-if="scope.row.success">成功</HStatus>
            <HStatus v-else type="error">失败</HStatus>
          </template>
        </el-table-column>
        <el-table-column prop="sendTime" label="发送时间" width="130" />
        <el-table-column label="失败原因" show-overflow-tooltip prop="error" min-width="100">
          <template #default="scope">
            <div style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis">
              {{ scope.row.error }}
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <Pagination
          ref="paginationRef"
          :list_total="total"
          @size-change="__getList"
          @current-change="__getList"
        />
      </div>
    </HModal>
  </div>
</template>

<style scoped lang="scss">
  .pagination {
    margin-top: 12px;
    display: flex;
    justify-content: flex-end;
  }
</style>
